package com.gitee.wsl.logger


/**
 * A [Logger] that transforms the logs of a given [logger] before outputting them.
 * @param logger the [Logger] whose results should be transformed
 * @param transformLogLevel When provided, changes the [LogLevel] using this transformation method
 * @param transformLogLevel When provided, changes the tag using this transformation method
 * @param transformLogLevel When provided, changes the [Throwable] using this transformation method
 * @param transformLogLevel When provided, changes the message using this transformation method
 */
open class TransformLogger(
    private val logger: Logger,
    private val transformLogLevel: ((LogLevel) -> LogLevel)? = null,
    private val transformTag: ((String?) -> String?)? = null,
    private val transformThrowable: ((Throwable?) -> Throwable?)? = null,
    private val transformMessage: ((Any?) -> String?)? = null,
) : Logger() {

    override fun log(
        level: LogLevel,
        message: Any?,
        throwable: Throwable?
    ) {
        val logLevel = transformLogLevel?.invoke(level) ?: level
        val logTag = transformTag?.invoke(tag) ?: tag
        val logThrowable = transformThrowable?.invoke(throwable) ?: throwable
        val logMessage = transformMessage?.invoke(message)?.let { { it } } ?: message
        logger.log(
            logLevel,
            "$logTag:$logMessage",
            logThrowable,
            // already resolve the lazy message if it needs transformation (else we cannot transform it)
            // after transform wrap it in a "lazy" closure again {{🤗}}
            //logMessage,
        )
    }
}